package com.ssplayer.custom;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.ssplayer.bean.Lyric;

/**
 * 2013/6/1
 * 
 * @author wwj 处理歌词的类
 */
public class LrcProcess {
	private List<Lyric> lyrics; // List集合存放歌词内容对象
	private Lyric lyric; // 声明一个歌词内容对象

	/**
	 * 无参构造函数用来实例化对象
	 */
	public LrcProcess() {
		lyrics = new ArrayList<Lyric>();
	}

	public List<Lyric> getLyrics() {
		return lyrics;
	}

	/**
	 * 读取歌词
	 * 
	 * @param path
	 * @return
	 */
	public void readLyric(String path) {
		File f = new File(path.replace(".mp3", ".lrc"));
		FileInputStream fis = null;
		BufferedReader br = null;
		try {
			// 创建一个文件输入流对象
			fis = new FileInputStream(f);
			// 根据不同的编码格式读取，防止乱码
			InputStreamReader isr = new InputStreamReader(fis, codeString(f));
			br = new BufferedReader(isr);
			String s = "";
			while ((s = br.readLine()) != null) {
				// 替换字符
				s = s.replace("[", "");
				s = s.replace("]", "@");
				// 分离“@”字符
				String splitLrcData[] = s.split("@");
				if (splitLrcData.length > 1) {
					// 新创建歌词内容对象
					lyric = new Lyric();

					lyric.setLyricContent(splitLrcData[1]);
					// 处理歌词取得歌曲的时间
					int lyricTime = time2Str(splitLrcData[0]);

					lyric.setLyricTime(lyricTime);

					// 添加进列表数组
					lyrics.add(lyric);

				}
			}
		} catch (Exception e) {
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 解析歌词时间 歌词内容格式如下：
	 * 
	 * @param timeStr
	 * @return
	 */
	public int time2Str(String timeStr) {
		timeStr = timeStr.replace(":", ".");
		timeStr = timeStr.replace(".", "@");

		String timeData[] = timeStr.split("@"); // 将时间分隔成字符串数组

		// 分离出分、秒并转换为整型
		int minute = Integer.parseInt(timeData[0]);
		int second = Integer.parseInt(timeData[1]);
		int millisecond = Integer.parseInt(timeData[2]);

		// 计算上一行与下一行的时间转换为毫秒数
		int currentTime = (minute * 60 + second) * 1000 + millisecond * 10;
		return currentTime;
	}

	/**
	 * 判断文件的编码格式
	 * 
	 * @return 文件编码格式
	 * @throws Exception
	 */
	public static String codeString(File file) throws Exception {
		FileInputStream fis = new FileInputStream(file);
		BufferedInputStream bin = new BufferedInputStream(fis);
		int charsetName = (bin.read() << 8) + bin.read();
		String code = null;

		switch (charsetName) {
		case 0x0a5b:
		case 0x5b74:
		case 0x5b30:
		case 0xefbb:
			code = "UTF-8";
			break;
		case 0xfffe:
			code = "Unicode";
			break;
		case 0xfeff:
			code = "UTF-16BE";
			break;
		case 0xffff:
			code = "UTF-16LE";
			break;
		default:
			code = "GBK";
			break;
		}
		bin.close();
		return code;
	}
}
